<?php

namespace Matrix\Controller;

class OrderController extends CommonController {
	public function __construct() {
		parent::__construct ();
		parent::__config ( array (
				'model_name' => 'Order' 
		) );
	}
	public function index() {
		parent::__config ( array (
				'fuzzy' => 'code,username' ,
				'between_time'=>'review_time',
				'statistics'=>'money',//需要统计求和字段
		) );
		
		$_GET ['order_statue'] = I ( 'order_statue' );
		$this->order_statue=I ( 'order_statue' );
		$table = I ( 'table' );
		if (! $table == null && ! empty ( $table )) {
			parent::__config ( array (
					'display' => 'table',
					'display_parms' => array (
							'table' => 1 
					) 
			) );
		}
		parent::index ();
		
	}
	
	// 审核通过
	function batch_summary() {
		$model = D ( "Model" );
		$model->startTrans ();
		// 获取订单套餐信息
		$where_find ['id'] = intval ( I ( 'id' ) );
		$where_find ['order_statue'] = array (
				'neq',
				2
		);
		$orderModel = D ( "Order" );
		$order_info = $orderModel->where ( $where_find )->find ();
		if ($order_info ['order_statue'] == 2) {
			$this->redirect ( 'Order/index', array (
					'order_statue' => 2 
			) );
		}
		
		if ($order_info) {
			switch ($order_info ['taocan_num']) {
				case 0 :
					$datetime = new \DateTime ();
					$datetime->modify ( "+1 day" );
					$time_num = $datetime->format ( 'U' ) - time (); //免费试用1天
					break;
				case 1 :
					$time = time (); // 当前时间戳
					$date = date('Y').'-'.(date('m',$time)+1).'-'.date( 'd H:i:s' ); // 一个月后日期
					$datetime = new \DateTime ( $date );
					$time_num = $datetime->format ( 'U' ) - time ();
					break;
				case 2 :
					$time = time (); // 当前时间戳
					$date = date ( 'Y', $time ) + 1 . '-' . date ( 'm-d H:i:s' ); // 一年后日期
					$datetime = new \DateTime ( $date );
					$time_num = $datetime->format ( 'U' ) - time ();
					break;
				case 5 :
					$time = time (); // 当前时间戳
					$date = date ( 'Y', $time ) + 100 . '-' . date ( 'm-d H:i:s' ); // 终身日期
					$datetime = new \DateTime ( $date );
					$time_num = $datetime->format ( 'U' ) - time ();
					break;
				case 6 :
					$datetime = new \DateTime ();
					$datetime->modify ( "+1 day" );
					$time_num = $datetime->format ( 'U' ) - time ();
					break;
				case 7 :
					$datetime = new \DateTime ();
					$datetime->modify ( "+" . $order_info ['day_num'] . " day" );
					$time_num = $datetime->format ( 'U' ) - time ();
					break;
				default :
					;
					break;
			}
			// 增加用户过期时间
			$userModel = D ( "User" );
			$where_user ['id'] = $order_info ['user_id'];
			$user_info = $userModel->where ( $where_user )->find ();
			// 判断若是免费试用，看是否已经试用过
			if ($order_info ['taocan_num'] == 6 || $order_info ['taocan_num'] == 0) {
				if ($user_info ['is_fee']) {
					$this->redirect ( 'Order/index', array (
							'order_statue' => 1 
					), 3, "该账号已免费试用一天已体验过" );
				}
			}
			if ($user_info ['end_time'] <= time ()) {
				$data ['end_time'] = time () + $time_num;
			} else {
				$data ['end_time'] = $user_info ['end_time'] + $time_num;
			}
			// 开启事物
			
			$data ['id'] = $order_info ['user_id'];
			if ($order_info ['taocan_num'] == 6 || $order_info ['taocan_num'] == 0) {
				$data ['is_fee'] = 1;
			}
			// dump($data);
			// die();
			$res_save_user = $userModel->save ( $data );
			// 修改订单状态
			$data_order ['order_statue'] = 2;
			$data_order ['review_time'] = time();
			$data_order ['id'] = intval ( I ( 'id' ) );
			$res_save_order = $orderModel->save ( $data_order );
			if ($res_save_user === false || $res_save_order === false) {
				$model->rollback ();
				$this->redirect ( 'Order/index', array (
						'order_statue' => 1 
				) );
			} else {
				$model->commit ();
				$this->redirect ( 'Order/index', array (
						'order_statue' => 2 
				) );
			}
		}
	}
	
	function batch_summary_list() {
		$ids = $_REQUEST ['checkboxes'];
		foreach ( $ids as $key_ids => $value_ids ) {
			// 获取订单套餐信息
			$where_find ['id'] = intval ( $value_ids );
			$where_find ['order_statue'] = array (
					'elt',
					1 
			);
			$orderModel = D ( "Order" );
			$order_info = $orderModel->where ( $where_find )->find ();
			//
			if ($order_info ['order_statue'] == 2) {
				$this->redirect ( 'Order/index', array (
						'order_statue' => 2 
				) );
			}
			
			if ($order_info) {
				switch ($order_info ['taocan_num']) {
					
					case 0 :
						$datetime = new \DateTime ();
						$datetime->modify ( "+1 day" );
						$time_num = $datetime->format ( 'U' ) - time ();
						break;
					case 1 :
						$time = time (); // 当前时间戳
						$date = date('Y').'-'.(date('m',$time)+1).'-'.date( 'd H:i:s' ); // 一个月后日期
						$datetime = new \DateTime ( $date );
						$time_num = $datetime->format ( 'U' ) - time ();
						break;
					case 2 :
						$time = time (); // 当前时间戳
						$date = date ( 'Y', $time ) + 1 . '-' . date ( 'm-d H:i:s' ); // 一年后日期
						$datetime = new \DateTime ( $date );
						$time_num = $datetime->format ( 'U' ) - time ();
						break;
					case 5 :
						$time = time (); // 当前时间戳
						$date = date ( 'Y', $time ) + 100 . '-' . date ( 'm-d H:i:s' ); // 一年后日期
						$datetime = new \DateTime ( $date );
						$time_num = $datetime->format ( 'U' ) - time ();
						break;
					case 6 :
						$datetime = new \DateTime ();
						$datetime->modify ( "+1 day" );
						$time_num = $datetime->format ( 'U' ) - time ();
						break;
					case 7 :
						$datetime = new \DateTime ();
						$datetime->modify ( "+" . $order_info ['day_num'] . " day" );
						$time_num = $datetime->format ( 'U' ) - time ();
						break;
					default :
						;
						break;
				}
				// 增加用户过期时间
				$userModel = D ( "User" );
				$where_user ['id'] = $order_info ['user_id'];
				$user_info = $userModel->where ( $where_user )->find ();
				// 判断若是免费试用，看是否已经试用过
				if ($order_info ['taocan_num'] == 6 || $order_info ['taocan_num'] == 0) {
					if ($user_info ['is_fee']) {
						$this->redirect ( 'Order/index', array (
								'order_statue' => 1 
						), 3, "该账号已免费试用一天已体验过" );
					}
				}
				if ($user_info ['end_time'] <= time ()) {
					$data ['end_time'] = time () + $time_num;
				} else {
					$data ['end_time'] = $user_info ['end_time'] + $time_num;
				}
				// 开启事物
				
				$data ['id'] = $order_info ['user_id'];
				if ($order_info ['taocan_num'] == 6 || $order_info ['taocan_num'] == 0) {
					$data ['is_fee'] = 1;
				}
				$res_save_user = $userModel->save ( $data );
				// 修改订单状态
				$data_order ['order_statue'] = 2;
				$data_order ['review_time'] = time();
				$data_order ['id'] = intval ( $value_ids );
				$res_save_order = $orderModel->save ( $data_order );
			}
		}
		$this->redirect ( 'Order/index', array (
				'order_statue' => 2 
		) );
	}
	function add() {
		parent::add ();
	}
	function insert() {
		parent::__config ( array (
				'display' => 'index' 
		) );
	}
	function foreverdelete() {
		parent::__config ( array (
				'display' => 'index?table=1' 
		) );
		parent::foreverdelete ();
	}
	function edit() {
		parent::__config ( array (
				'display' => 'index' 
		) );
		$data ['id'] = I ( 'id' );
		$data ['status'] = 1;
		parent::update ( $data );
		// parent::edit ();
	}
	function update() {
		parent::__config ( array (
				'display' => 'index' 
		) );
		$data ['id'] = I ( 'id' );
		$data ['status'] = 0;
		parent::update ( $data );
	}
	function logout() {
		session ( "bm_ouradmin", null );
		session ( 'order', null );
		$this->redirect ( 'Home/Index/index' );
	}
	function bean() {
		parent::bean ();
	}
	function sort() {
		parent::sort ();
	}
	function excelImport() {
	}
	function excelOutput() {
	}
}